package com.xtayfjpk.zookeeper.test.master_workers.callback.master;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;

import com.xtayfjpk.zookeeper.test.master_workers.Master;
import com.xtayfjpk.zookeeper.test.master_workers.callback.BaseChildrenCallback;

public class WorkerAssignmentCallback extends BaseChildrenCallback {
	private static final Logger LOGGER = Logger.getLogger(WorkerAssignmentCallback.class);
	private Master master;
	
	public WorkerAssignmentCallback(Master master) {
		this.master = master;
	}
	
	@Override
	protected void connectionLossCaseAction(int rc, String path, Object ctx, List<String> children) {
		master.getAbsentWorkerTasks(path);
	}
	
	@Override
	protected void okCaseAction(int rc, String path, Object ctx, List<String> children) {
		LOGGER.info("Successfully got a list of assignment: "
				+ children.size() + " tasks");
		
		for(String task : children) {
			master.getDataReassign(path + "/" + task, task);
		}
	}
	
	@Override
	protected void defaultCaseAction(int rc, String path, Object ctx,
			List<String> children) {
		LOGGER.error("getChildren failed",  KeeperException.create(Code.get(rc), path));
	}
}
